/**
 * @file models/brinson.js
 * @author
 */
import { routerRedux } from 'dva/router';
import api from '../api';

export default {
	namespace: 'brinson',
	state: {
		timeRange:'0',
		cBenchCode:'',
		indunum:'38_1',
		lPortId:'',
		beginDate:'',
		endDate:'',
		pdtsearchId:'',//产品模糊搜索id
    isShow:true,
    onePdt:''
	},
	subscriptions : {
		setup({dispatch, history}) {
			history.listen(({ pathname, query }) => {
				// 没有参数时，向后端请求数据，并且改变query
				if( pathname === '/achievement/equity/brinson' ){
					dispatch({type: 'whetherRequest',payload:query,pathname})
					dispatch({
						type:'getListPdt',
						payload:{
							'productType':['LICAI','GONGMU','MONI','SIMU']
						}
					})
					dispatch({
						type:'getBListPdt',
						
					})
				}
			})
		}
	},
	effects : {

		* whetherRequest({
				payload,pathname
			}, { put,select}) {

				const {
					lPortId,
					timeRange,
					cBenchCode,
					indunum,
					beginDate,
					endDate
				}=payload
				if(lPortId||timeRange||cBenchCode||indunum||beginDate||endDate){
					if(!(lPortId&&cBenchCode&&indunum&&((timeRange=='0'&&beginDate&&endDate)||(timeRange!='0')))){
						yield put({
							type:'changeRouter',
							payload:{
								pathname
							}
						})
					}else{
						yield put({
			        type:'setState',
			        payload
			      })
					}
				}
		},

		* changeRouter({payload}, {put}){
			yield put(routerRedux.push(payload))
		},
		*getBListPdt({ payload:query }, { call, put }) {
        const response = yield call(api.benchmarkMgt.getBenchmarks, query);//基准列表
        const benchmarksList = []
        response.data.map((i)=>{
        	i.benchList.map((k)=>{
        		benchmarksList.push(k)
        	})
        })
        console.log(32222222,response)
        const productList = benchmarksList.map((item, i) => ({
          id: item.benchId + '',
          value: `${item.benchCode}：${item.benchName}`
        }))
        yield put({
          type: 'getBListPdtSuccess',
          payload:productList,
        });
      },
		* getListPdt({ payload:query }, { call, put }) {
        const response = yield call(api.getBrinsonPdt, query);
        const productList = response.data.map((item, i) => ({
          id: item.productId + '',
          value: `${item.productCode}：${item.productName}`
        }))
        yield put({
          type: 'getListPdtSuccess',
          payload:productList,
        });
      },
    * getOnePdt({payload},{call,put}){
      const pdtId = payload.pdtId
      const response = yield call(api.getOnePdt,pdtId);
      yield put({
        type: 'getOnePdtSuccess',
        payload: response
      });
    },

	},
	reducers : {
		setState(state, {payload}) {
			return {
				...state,
				...payload
			}
		},

		clearState(state) {
      return{
				timeRange:'0',
				cBenchCode:'',
				indunum:'38_1',
				lPortId:'',
				beginDate:'',
				endDate:'',
				pdtsearchId:'',

			}
    },
    getBListPdtSuccess(state,{payload}){
    	const data = payload;
      return {
        ...state,
        BListPdt:data
      }
    },
    getListPdtSuccess(state,{payload}){
      const data = payload;
      return {
        ...state,
        ListPdt:data
      }
    },
    getOnePdtSuccess(state,{payload}){
    	const data = payload.data
    	const pdtN = data?`${data.productCode}:${data.productName}`:null;
    	return {
    		...state,
    		onePdt:pdtN
    	}
    },

	}
};
